package com.etone.smartaudit.wopi.controller;

import com.etone.smartaudit.wopi.domian.FileInfo;
import com.etone.smartaudit.wopi.service.WopiService;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author: sovy
 * @date: 2021/1/28
 * @description:
 */
@AllArgsConstructor
@RestController
public class WopiController {
    private WopiService wopiService;

    /**
     * search a file from the host, return a file’s binary contents
     */
    @GetMapping("/files/{name}/contents")
    public void getFile(@PathVariable String name, HttpServletResponse response) {
        wopiService.getFile(name, response);
    }

    /**
     * The postFile operation updates a file’s binary contents.
     */
    @PostMapping("/files/{name}/contents")
    public void postFile(@PathVariable(name = "name") String name, @RequestBody byte[] content,
                         HttpServletRequest request) {
        wopiService.postFile(name, content, request);
    }


    /**
     * returns information about a file, a user’s permissions on that file,
     * and general information about the capabilities that the WOPI host has on the file.
     */
    @GetMapping("/files/{name}")
    public ResponseEntity<FileInfo> checkFileInfo(
            @PathVariable(name = "name") String name,
            @RequestParam(required = false) String userName
            , HttpServletRequest request) throws Exception {
        return wopiService.getFileInfo(name, userName, request);
    }

//    /***
//     * 根据指定路径获取文件
//     * @param name
//     * @param path
//     * @param userName
//     * @return
//     * @throws Exception
//     */
//    @GetMapping("/files/path/{name}")
//    public ResponseEntity<FileInfo> getPathFileInfo(
//            @PathVariable(name = "name") String name,
//             @RequestParam(required = false) String path,
//            @RequestParam(required = false) String userName) throws Exception {
//        return wopiService.getPathFileInfo(name, userName,path);
//    }

    /**
     * Handling lock related operations
     */
    @PostMapping("/files/{name}")
    public ResponseEntity handleLock(@PathVariable(name = "name") String name, HttpServletRequest request) {
        return wopiService.handleLock(name, request);
    }

}
